package com.yungu.swift.demo.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Zerrion
 */
@Configuration
public class AmqpConfig {

    @Value("${log.queue.name}")
    private String logQueue;

    @Value("${log.exchange.name}")
    private String logExchange;

    @Value("${log.routing.key.name}")
    private String logRoutingKey;

    @Bean(name = "logQueue")
    public Queue logQueue() {
        return new Queue(logQueue, true);
    }

    @Bean
    public DirectExchange logExchange() {
        return new DirectExchange(logExchange, true, false);
    }

    @Bean
    public Binding logBinding() {
        return BindingBuilder.bind(logQueue()).to(logExchange()).with(logRoutingKey);
    }

    /*---------test demo ---------*/

    @Bean
    public Queue loginLogQueue() {
        return new Queue("loginLogQueue", true);
    }

    @Bean
    public TopicExchange loginLogExchange() {
        return new TopicExchange("loginLogExchange");
    }

    @Bean
    public Binding loginLogBinding() {
        return BindingBuilder.bind(loginLogQueue()).to(loginLogExchange()).with("loginLog.*");
    }



    /* demo */

    /**
     * 声明队列
     */
    @Bean
    public Queue bootQueue() {
        return new Queue("bootQueue");
    }

    /**
     * 声明 Exchange
     */
    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange("directExchange");
    }

    @Bean
    public TopicExchange topicExchange() {
        return new TopicExchange("topicExchange");
    }

    @Bean
    public HeadersExchange headersExchange() {
        return new HeadersExchange("headersExchange");
    }

    @Bean
    public FanoutExchange fanoutExchange() {
        return new FanoutExchange("fanoutExchange");
    }

    /**
     * 声明 Binding
     */
    @Bean
    public Binding bindingDirectExchange(Queue bootQueue, DirectExchange directExchange) {
        return BindingBuilder.bind(bootQueue).to(directExchange).with("bootQueue");
    }

    @Bean
    public Binding bindingTopicExchange(Queue bootQueue, TopicExchange topicExchange) {
        return BindingBuilder.bind(bootQueue).to(topicExchange).with("routing.*");
    }

    @Bean
    public Binding bindingHeadersExchange(Queue bootQueue, HeadersExchange headersExchange) {
        Map<String, Object> map = new HashMap<>();
        map.put("betty", "rubble");
        map.put("barney", "rubble");
        //whereAny表示部分匹配，whereAll表示全部匹配
        // return BindingBuilder.bind(bootQueue).to(headersExchange).whereAll(map).match();
        return BindingBuilder.bind(bootQueue).to(headersExchange).whereAny(map).match();
    }

    @Bean
    public Binding FanoutExchange(Queue bootQueue, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(bootQueue).to(fanoutExchange);
    }


}
